$(function() {

    var index = parent.layer.getFrameIndex(window.name);
    parent.layer.iframeAuto(index);

    $(".select-half-halfBom").on("click", function() {
        var id = $(this).data("id");
        if (parent.layui.$("input[name='halfIds'][value='" + id + "']").size() == 0) {
            var unitName = $(this).data("unitname");
            var str = '<tr><td><input type="hidden" name="halfIds" value="' + id + '" />' + $(this).data('code') + '</td><td>';
            str += $(this).data('name') + '</td><td>' + unitName + '</td>';
            str += '<td><input type="text" name="halfScale" autocomplete="off" class="layui-input" style="width: 100px; display: inline-block" />' + $(this).data("unitstr") + '</td>';
            str += '<td><a href="javascript:void(0);" class="layui-btn layui-btn-xs layui-btn-danger hidden-xs delete-row" id="' + id + '"><i class="layui-icon">&#xe640;</i></a></td></tr>';
            parent.layui.$("#halfTable").append(str);
        } else {
            parent.layer.tips("已选择", ".delete-row[id='" + id + "']", {
                time : 3000
            });
        }
        parent.layer.close(index);
    });

    $(".select-half-halfHalfBom").on("click", function() {
        var id = $(this).data("id");
        if (id == parent.layui.$(":hidden[name='id']").val()) {
            parent.layer.msg("半成品BOM配置不能选择自身");
        } else if (parent.layui.$("input[name='halfIds'][value='" + id + "']").size() == 0) {
            var unitName = $(this).data("unitname");
            var str = '<tr><td><input type="hidden" name="halfIds" value="' + id + '" />' + $(this).data('code') + '</td><td>';
            str += $(this).data('name') + '</td><td>' + unitName + '</td>';
            str += '<td><input type="text" name="halfScale" autocomplete="off" class="layui-input" style="width: 100px; display: inline-block" />' + $(this).data("unitstr") + '</td>';
            str += '<td><a href="javascript:void(0);" class="layui-btn layui-btn-xs layui-btn-danger hidden-xs delete-row" id="' + id + '"><i class="layui-icon">&#xe640;</i></a></td></tr>';
            parent.layui.$("#halfTable").append(str);
        } else {
            parent.layer.tips("已选择", ".delete-row[id='" + id + "']", {
                time : 3000
            });
        }
        parent.layer.close(index);
    });

    $(".select-half-pfmea").on("click", function() {
        var modelId = parent.$("#modelId").val();
        var id = $(this).data("id");
        if (parent.$("#halfId").val() != id) {
            var url = "/fmea/pfmea_half/step1/" + modelId + "?halfId=" + id;
            parent.window.location.href = url;
        } else {
            parent.layer.tips("已选择", "input[name='halfCode'][id='" + id + "']", {
                time : 3000
            });
        }
        parent.layer.close(index);
    });

});